﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace Runda.CMS.Common.Utility
{
    /// <summary>
    /// 操作返回结果
    /// </summary>
    public class Export
    {
        static public void Export2Excel(System.Data.DataTable dtData, string FileName, HttpResponse response)
        {
            System.Web.UI.WebControls.DataGrid output = new DataGrid();
            output.ItemDataBound += new DataGridItemEventHandler(output_ItemDataBound);
           
            output.DataSource = dtData;
            output.DataBind();
            response.Clear();
            response.Buffer = true;
            response.Charset = "GB2312";
            //response.AppendHeader("Content-Disposition", "attachment;filename=HelloAdmin.xls");
            response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            response.ContentType = "application/vnd.xls";//设置输出文件类型为excel文件。 

            response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
            response.AppendHeader("content-disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");
            //response.ContentType = "text/csv";
            //response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".csv"); //定义输出文件和文件名

            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            output.RenderControl(oHtmlTextWriter);
            response.Write(oStringWriter.ToString());
            response.End();
        }

        static void output_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            for (int iIndex = 0; iIndex < e.Item.Cells.Count; iIndex++)
            {
                e.Item.Cells[iIndex].Attributes.Add("style", "vnd.ms-excel.numberformat: @");
            }
        }

        public void doExport(DataTable dtData, string FileName, DataControlFieldCollection fields)
        {
            // 当前对话
            System.Web.HttpContext curContext = System.Web.HttpContext.Current;
            // IO用于导出并返回excel文件
            System.IO.StringWriter strWriter = null;

            if (dtData != null)
            {
                // 设置编码和附件格式
                System.Web.HttpContext.Current.Response.Clear();

                HttpContext.Current.Response.Buffer = true;
                string fileName = HttpUtility.UrlEncode(FileName, System.Text.Encoding.GetEncoding("GB2312"));
                curContext.Response.ContentType = "text/csv";
                curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".csv"); //定义输出文件和文件名
                // 导出excel文件
                strWriter = new System.IO.StringWriter();
                if (dtData.Rows.Count != 0)
                {
                    curContext.Response.ContentEncoding = System.Text.Encoding.Default;
                    curContext.Response.Charset = "GB2312";

                    foreach (DataControlField field in fields)
                    {
                        if (!string.IsNullOrEmpty(field.HeaderText))
                        {
                            strWriter.Write(field.HeaderText + ",");
                        }
                    }
                    strWriter.Write("\n");

                    foreach (DataRow dr in dtData.Rows)
                    {
                        for (int i = 0; i < dtData.Columns.Count; i++)
                        {
                            strWriter.Write("\"" + dr[i].ToString() + "\"" + ",");
                        }
                        strWriter.Write("\n");
                    }

                }
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.Flush();
                curContext.Response.End();
            }
        }


    }

}